var express = require('express');
var router = express.Router();
var request = require('request');

var CommonJS = require('../../../common/common');
var DBConfig = require('../../../db/dbconfig');
var mysql = require('mysql');
var pool = mysql.createPool(DBConfig.mysql2);

// 添加收藏
router.get('/jkyx/goods_collection', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户account_id
	var account_id = param.account_id || '';
	// 商品ID
	var goods_id = param.goods_id || '';
	// 是否收藏  1:收藏 2:取消收藏
	var collection_type = param.collection_type || '1';
	if (account_id == '' || goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入正确的参数',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getgoodsCollection,[account_id,goods_id],function (err, results) {
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				// 没有收藏过这个商品
				if (results.length == 0) {
					var insertSQL = 'insert into collection (account_id,goods_id,status,create_time) values (?,?,?,?)';
					connection.query(insertSQL,[account_id,goods_id,collection_type,CommonJS.getNowFormatDate()],function (err, results) {
						connection.release();
						if (err){
						  	res.end(JSON.stringify({
				     			msg: '数据库查询失败',
				     			status:'103',
				     			err: err
				     		}));
				     	}else{
				     		res.end(JSON.stringify({
				     			msg: '收藏成功',
				     			status:'100',
								type: '1'
				     		}));
				     	}
				    })
				} else{// 有收藏过这个商品
					connection.query(DBConfig.goodsCollection,[collection_type,account_id,goods_id],function (err, results) {
						connection.release();
						if (err){
						  	res.end(JSON.stringify({
				     			msg: '数据库查询失败',
				     			status:'103',
				     			err: err
				     		}));
				     	}else{
				     		if (collection_type == '1') {
				     			res.end(JSON.stringify({
					     			msg: '收藏成功',
					     			status:'100',
									type: '1'
					     		}));
				     		} else{
				     			res.end(JSON.stringify({
					     			msg: '取消收藏',
					     			status:'100',
									type: '2'
					     		}));
				     		}
				     	}
				    })
				} 
			}
		})
	})
});

// 用户收藏
router.get('/jkyx/collection_list', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || '';
	if (account_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入用户id',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.getgoodsCollections,[account_id,account_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				var tempGoods = [];
				// 处理商品 和 规格
				var goods = results[0];
				var goods_len = goods.length;
				var specs = results[1];
				var spec_len = specs.length;
				for (var i = 0; i < goods_len; i ++) {
					var salecount = 0;
					var spec = [];
					var stock = 0;
					for (var j = 0; j < spec_len; j ++) {
						if (specs[j].goods_id == goods[i].id) {
							salecount = salecount + specs[j].salecount;
							stock = stock + specs[j].stock;
							spec.push({
								id: specs[j].id,
								salecount: specs[j].salecount,
								price: specs[j].price / 100,
								spec: specs[j].spec,
								origprice: specs[j].origprice,
								erpcode: specs[j].erpcode,
								stock: specs[j].stock,
							})
						}
					}
					goods[i].salecount = salecount;
					goods[i].stock = stock;
					goods[i].spec = spec;
					goods[i].imgs = JSON.parse(goods[i].imgs);
					tempGoods.push(goods[i])
				}
				
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     			goods: tempGoods.reverse(),
	     		}));	         
			}
		})
	})
});
// 用户 删除某个 足迹
router.get('/jkyx/collection_delete_one', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || '';
	// 商品ID
	var goods_id = param.goods_id || '';
	if (account_id == '' || goods_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入正确的参数',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.deleteCollectionByID,[account_id,goods_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});
// 用户 删除全部 收藏
router.get('/jkyx/collection_deleteAll', function(req, res, next) {
	var param = req.query || req.params; 
	// 用户ID
	var account_id = param.account_id || '';
	if (account_id == '') {
		res.end(JSON.stringify({
 			msg: '请传入正确的参数',
 			status:'105',
 		}));
 		return;
	}
    pool.getConnection(function(err, connection) {
		if(err){
			res.end(JSON.stringify({
     			msg: '数据库连接失败',
     			status:'101',
     		}));
		}
		connection.query(DBConfig.deleteCollection,[account_id],function (err, results) {
			connection.release();
			if (err){
			  	res.end(JSON.stringify({
	     			msg: '数据库查询失败',
	     			status:'102',
	     			err: err
	     		}));
			}else{
				res.end(JSON.stringify({
	     			msg: '操作成功',
	     			status:'100',
	     		}));	         
			}
		})
	})
});
module.exports = router;
